package com.swa99ylum.pjv.validator;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.swa99ylum.pjv.exception.JSONValidateException;

/** <h1>正则 验证器</h1>
<br> 会先将 value 转成 字符串类型，然后 验证 其 是否能 匹配 正则表达式
<br>    
<br>    
 */
public class PatternValidator extends Validator {

	private String regex;
	
	protected PatternValidator() {
	}
	
	public PatternValidator(String regex) {
		super();
		this.regex = regex;
	}

	protected boolean match(String regex, String value) {
		Pattern pattern1 = Pattern.compile(regex);
		Matcher matcher1 = pattern1.matcher(value);
		
		return matcher1.matches();
	}
	
	@Override
	public void doValidate(Object rootObject, String[] nodePath, String nodeName, Map<String, Object> parentValue, Object value) throws JSONValidateException {
		
		if(match(regex, String.valueOf(value))) return;
		throw new JSONValidateException(nodePath, value, "不满足 正则表达式：" + regex); 
	}
	
	
	public static void main(String[] args) {
		String aaa111 = "123";
		
		System.out.println(aaa111.equals("123"));

		Pattern pattern1 = Pattern.compile("123"); //匹配 @{...}
		Matcher matcher1 = pattern1.matcher(aaa111);
		
		if (matcher1.matches()) {//javaBean 的 属性
			
//				System.out.println(0 + "::\"" + matcher1.group(0) + "\"");
//				System.out.println(1 + "::\"" + matcher1.group(1) + "\"");
//				System.out.println(2 + "::\"" + matcher1.group(2) + "\"");
			
		}
	}
	
	
	
	public String getRegex() {
		return regex;
	}

	public void setRegex(String regex) {
		this.regex = regex;
	}
	
}
